// utils.js - 工具函数
class Utils {
    // 日期格式化
    static formatDate(dateString, format = 'YYYY-MM-DD HH:mm:ss') {
        const date = new Date(dateString);
        if (isNaN(date.getTime())) {
            return '无效日期';
        }

        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');

        return format
            .replace('YYYY', year)
            .replace('MM', month)
            .replace('DD', day)
            .replace('HH', hours)
            .replace('mm', minutes)
            .replace('ss', seconds);
    }

    // 表单验证
    static validateForm(formData, rules) {
        const errors = {};
        let isValid = true;

        for (const field in rules) {
            const value = formData[field];
            const fieldRules = rules[field];

            // 检查必填
            if (fieldRules.required && (!value || value.trim() === '')) {
                errors[field] = fieldRules.requiredMessage || `${field}不能为空`;
                isValid = false;
            }

            // 检查最小长度
            if (fieldRules.minLength && value.length < fieldRules.minLength) {
                errors[field] = fieldRules.minLengthMessage || `${field}长度不能少于${fieldRules.minLength}个字符`;
                isValid = false;
            }

            // 检查最大长度
            if (fieldRules.maxLength && value.length > fieldRules.maxLength) {
                errors[field] = fieldRules.maxLengthMessage || `${field}长度不能超过${fieldRules.maxLength}个字符`;
                isValid = false;
            }

            // 检查正则表达式
            if (fieldRules.pattern && !fieldRules.pattern.test(value)) {
                errors[field] = fieldRules.patternMessage || `${field}格式不正确`;
                isValid = false;
            }
        }

        return {
            isValid,
            errors
        };
    }

    // 本地存储工具
    static storage = {
        set(key, value) {
            try {
                localStorage.setItem(key, JSON.stringify(value));
                return true;
            } catch (error) {
                console.error('存储失败:', error);
                return false;
            }
        },

        get(key) {
            try {
                const value = localStorage.getItem(key);
                return value ? JSON.parse(value) : null;
            } catch (error) {
                console.error('获取失败:', error);
                return null;
            }
        },

        remove(key) {
            try {
                localStorage.removeItem(key);
                return true;
            } catch (error) {
                console.error('删除失败:', error);
                return false;
            }
        },

        clear() {
            try {
                localStorage.clear();
                return true;
            } catch (error) {
                console.error('清除失败:', error);
                return false;
            }
        }
    };

    // 显示通知
    static showNotification(message, type = 'info', duration = 3000) {
        // 创建通知元素
        const notification = document.createElement('div');
        notification.className = `notification notification-${type}`;
        notification.textContent = message;

        // 添加到文档
        document.body.appendChild(notification);

        // 自动移除
        setTimeout(() => {
            notification.style.opacity = '0';
            setTimeout(() => {
                document.body.removeChild(notification);
            }, 300);
        }, duration);
    }

    // 隐藏通知
    static hideNotification() {
        const notifications = document.querySelectorAll('.notification');
        notifications.forEach(notification => {
            notification.style.opacity = '0';
            setTimeout(() => {
                document.body.removeChild(notification);
            }, 300);
        });
    }

    // 确保Utils全局可用
    static init() {
        if (!window.Utils) {
            window.Utils = Utils;
        }
    }
}

// 初始化Utils
Utils.init();

